package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbUserAdvanceDate;
import com.example.ymm.modules1.mapper.my.user.DbUserAdvanceDateMapper;
import com.example.ymm.modules1.query.DbUserAdvanceDateQueryCriteria;
import com.example.ymm.modules1.service.dto.UserAdvanceDateDto;
import com.example.ymm.modules1.service.user.DbUserAdvanceDateService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class DbUserAdvanceDateServiceImpl extends BaseServiceImpl<DbUserAdvanceDateMapper, DbUserAdvanceDate> implements DbUserAdvanceDateService {

    @Override
    public TableList<DbUserAdvanceDate> list(DbUserAdvanceDateQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DbUserAdvanceDate> page = new Page<DbUserAdvanceDate>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbUserAdvanceDate.class, criteria);
//        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<DbUserAdvanceDate> tableList=new TableList(page);
        return tableList;
    }

    public List<DbUserAdvanceDate> queryAll(DbUserAdvanceDateQueryCriteria criteria) {
        List<DbUserAdvanceDate> result =baseMapper.selectList(QueryHelpPlus.getPredicate(DbUserAdvanceDate.class, criteria));
        return result;
    }


    @Override
    public List<UserAdvanceDateDto> getDayListCount(DbUserAdvanceDateQueryCriteria criteria) {
        return baseMapper.getDayListCount(criteria);
    }


    @Override
    public void addBatchFn(List<UserAdvanceDateDto> dtos) {
        for(UserAdvanceDateDto dto:dtos){

            String userId=getUserId();
            String userName = getUserName();

            DbUserAdvanceDate advanceDate=new DbUserAdvanceDate();
            advanceDate.setDay(dto.getDay());
            advanceDate.setUserId(userId);
            advanceDate.setUserName(userName);
            addOneFn(advanceDate);

        }
    }


    @Override
    public void addOneFn(DbUserAdvanceDate advanceDate) {
        DbUserAdvanceDateQueryCriteria advanceCriteria=new DbUserAdvanceDateQueryCriteria();
        advanceCriteria.setDay(advanceDate.getDay());
//        String userName = getUserName();
        advanceCriteria.setUserId(advanceDate.getUserId());

        List<DbUserAdvanceDate> dbUserAdvanceDates = queryAll(advanceCriteria);
        if(UtilValidate.isEmpty(dbUserAdvanceDates)){
            DbUserAdvanceDate userAdvanceDate=new DbUserAdvanceDate();
            userAdvanceDate.setUserId(advanceDate.getUserId());
            userAdvanceDate.setUserName(advanceDate.getUserName());
            userAdvanceDate.setDay(advanceDate.getDay());
            saveOrUpdate(userAdvanceDate);
        }


    }
}
